<route>
{
  "meta":{
    "title":"升级通知",
    "showMenu":true,
    "serialNum": 40,
    "localism": "sys.upgrade_notification"
  }
}
</route>
<template>
  <div>
    <el-row :gutter="12">
      <el-col :span="6">
        <el-select v-model="queryParams.typeId" style="width: 100%" :placeholder="$t('sys.upgrade_notification_type')" clearable>
          <el-option
            v-for="(t, index) in types"
            :key="index"
            :value="t"
            :label="t">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="queryParams.description"
          :placeholder="$t('sys.description')"
          clearable
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="loadData(1)">{{$t('sys.query')}}</el-button>
        <el-button @click="onReset">{{$t('sys.reset')}}</el-button>
      </el-col>
      <el-col class="mt10">
        <el-button v-if="VUE_APP_USER !== 'APP'" type="primary" @click="$router.push({ name: 'messageCenter-escalate-detail' })">{{$t('sys.new')}}</el-button>
      </el-col>
    </el-row>

    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="code" :label="$t('sys.upgrade_notification_CODE')"/>
      <el-table-column prop="type" :label="$t('sys.upgrade_notification_type')"/>
      <el-table-column prop="description" :label="$t('sys.description')"/>
      <el-table-column prop="actions" :label="$t('sys.operation')">
        <template slot-scope="scope">
          <template>
            <span class="table-actionItem">
              <el-button type="text" @click="$router.push({ name: 'messageCenter-escalate-detail', query: { id: scope.row.id } })">{{$t('sys.edit')}}</el-button></span>
            <span v-if="VUE_APP_USER!=='APP'" class="table-actionItem">
              <el-popconfirm
                :title="$t('sys.confirm_to_delete_upgrade_notification')"
                placement="top"
                @confirm="deleteEscalate(scope.row.id)"
              >
                <el-button class="link-danger" type="text" slot="reference">{{$t('sys.delete')}}</el-button>
              </el-popconfirm>
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>
  </div>
</template>

<script>
import { getMsgUpgradenoticeTypes,getMsgUpgradenoticePagelist,deleteMsgUpgradenotice } from '@/apis/paas/UpgradeNoticeController.js'
export default {
  name: 'Escalate',
  components: {

  },
  data () {
    return {
      VUE_APP_USER: process.env.VUE_APP_USER,
      types: [],
      queryParams: {},
      loading: false,
      tableData: [],
      parsedData: [],
      pageData: {
        pageSize: 10,
        pageNo: 1
      },
      total: 0
    }
  },
  computed: {
    trimedQueryParams () {
      const params = Object.assign({}, this.queryParams)
      for (const prop in params) {
        if (params.hasOwnProperty(prop) && typeof params[prop] === 'string') {
          params[prop] = params[prop].trim()
        }
      }
      return params
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      this.getTypeList()
      this.loadData()
    },

    getTypeList () {
      getMsgUpgradenoticeTypes().then(res => {
        this.types = res || []
      })
    },

    loadData (pageNo = this.pageData.pageNo) {
      this.loading = true
      getMsgUpgradenoticePagelist({
        ...this.queryData,
        ...this.trimedQueryParams,
        ...this.pageData,
        pageNo
      }).then(res => {
        if (res.data.length === 0 && pageNo > 1) {
          this.loadData(pageNo - 1)
          return
        }
        const data = res.data.map(ele => {
          if (!ele.description) {
            ele.description = ''
          }
          return ele
        })
        this.tableData = this.parsedData = data
        this.total = res.totalCount
        this.pageData = {
          pageSize: res.pageSize,
          pageNo: res.pageNo
        }
      }).finally(() => {
        this.loading = false
      })
    },

    deleteEscalate (id) {
      deleteMsgUpgradenotice({ id }).then(res => {
        this.loadData()
        this.$message.success(this.$t('sys.operation_success'))
      })
    },

    onReset () {
      this.queryParams = {}
      this.loadData(1)
    }
  }
}
</script>

<style></style>
